import { _decorator, Component, director, instantiate, Node, Prefab } from 'cc';
import { NetWorkManager } from '../Global/NetWorkManager';
import { ApiMsgEnum, IApiPlayerListRes, IApiRoomListRes } from '../Common';
import { PlayerMgr } from '../UI/PlayerMgr';
import DataManager from '../Global/DataManager';
import { EventEnum, SceneEnum } from '../Enum';
import { RoomMgr } from '../UI/RoomMgr';
import EventManager from '../Global/EventManager';
const { ccclass, property } = _decorator;

@ccclass('HallMgr')
export class HallMgr extends Component {
    @property({type:Node})
    playerContainer:Node;
    @property({type:Prefab}) 
    playerPrefab:Prefab;

    @property({type:Node})
    roomContainer:Node;
    @property({type:Prefab}) 
    roomPrefab:Prefab;

    start() {
        director.preloadScene(SceneEnum.Room)
        EventManager.Instance.on(EventEnum.RoomJoin,this.handleJoinRoom,this)
        NetWorkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList,this.renderRoom,this);
        NetWorkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList,this.renderPlayers,this);
        this.playerContainer.destroyAllChildren();
        this.roomContainer.destroyAllChildren();
        this.getPlayers();
        this.getRooms();
    }

    protected onDestroy(): void {
        EventManager.Instance.off(EventEnum.RoomJoin,this.handleJoinRoom,this)
        NetWorkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoomList,this.renderRoom,this);
        NetWorkManager.Instance.unlistenMsg(ApiMsgEnum.MsgPlayerList,this.renderPlayers,this);
    }

    async getPlayers(){
        const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiPlayerList,{})
        if(!success){
            console.log("getPlayers error!",error);
            return;
        }
        console.log("res!",res);    
        this.renderPlayers(res);
    }

    async getRooms(){
        const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiRoomList,{})
        if(!success){
            console.log("getPlayers error!",error);
            return;
        }
        console.log("res!",res);    
        this.renderRoom(res);
    }
    renderRoom({list}:IApiRoomListRes){
        for(const c of this.roomContainer.children){
            c.active = false;
        } 
        while(list.length>this.roomContainer.children.length){
            const roomNode = instantiate(this.roomPrefab);
            roomNode.setParent(this.roomContainer); 
        }
        for(let i=0;i<list.length;i++){
            const data = list[i];
            const room = this.roomContainer.children[i];
            room.getComponent(RoomMgr).init(data) 
        }

    }

    renderPlayers({list}:IApiPlayerListRes){
        for(const c of this.playerContainer.children){
            c.active = false;
        }
        while(list.length>this.playerContainer.children.length){
            const playerNode = instantiate(this.playerPrefab);
            playerNode.setParent(this.playerContainer);
        }
        for(let i=0;i<list.length;i++){
            const data = list[i];
            const player = this.playerContainer.children[i];
            player.getComponent(PlayerMgr).init(data)
        }
    }

    async handleCreateRoom(){
        const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiRoomCreate,{}) 
        if(!success){
            console.log("createRoom error!",error);
            return;
        }

        DataManager.Instance.roomInfo = res.room;
        console.log("createRoom success!",DataManager.Instance.roomInfo);
        director.loadScene(SceneEnum.Room);
    }

    async handleJoinRoom(rid:number){
        const {success,error,res} = await NetWorkManager.Instance.callApi(ApiMsgEnum.ApiRoomJoin,{rid}) 
        if(!success){
            console.log("JoinRoom error!",error);
            return;
        }

        DataManager.Instance.roomInfo = res.room;
        console.log("JoinRoom success!",DataManager.Instance.roomInfo);
        director.loadScene(SceneEnum.Room);
    }
}


